<?php
/**
 * 文件详情设置页面
 */

require_once 'config/config.php';
require_once 'includes/db.php';

// 检查是否已安装
if (!file_exists(__DIR__ . '/config/installed.lock')) {
    header('Location: install.php');
    exit;
}

// 检查管理员登录
if (!$_SESSION['admin_logged_in']) {
    header('Location: login.php');
    exit;
}

$fileId = $_GET['id'] ?? null;
if (!$fileId) {
    header('Location: index.php');
    exit;
}

$message = '';
$messageType = '';

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) {
        $message = '安全验证失败';
        $messageType = 'error';
    } else {
        try {
            $db = new Database();

            $data = [
                'file_title' => trim($_POST['file_title'] ?? ''),
                'file_description' => trim($_POST['file_description'] ?? ''),
                'page_title' => trim($_POST['page_title'] ?? ''),
                'page_description' => trim($_POST['page_description'] ?? '')
            ];

            $db->saveFileDetails($fileId, $data);

            $message = '保存成功！3秒后返回文件列表';
            $messageType = 'success';
            header("Refresh: 3; url=index.php");

        } catch (Exception $e) {
            $message = '保存失败: ' . $e->getMessage();
            $messageType = 'error';
        }
    }
}

try {
    $db = new Database();
    $file = $db->getFileDetails($fileId);

    if (!$file) {
        header('Location: index.php');
        exit;
    }
} catch (Exception $e) {
    $message = '加载文件信息失败: ' . $e->getMessage();
    $messageType = 'error';
}

/**
 * 获取文件图标
 */
function getFileIcon($ext) {
    $icons = [
        'pdf' => '📄', 'doc' => '📝', 'docx' => '📝', 'txt' => '📃',
        'jpg' => '🖼️', 'jpeg' => '🖼️', 'png' => '🖼️', 'gif' => '🖼️',
        'zip' => '🗜️', 'rar' => '🗜️', '7z' => '🗜️',
        'mp4' => '🎬', 'avi' => '🎬', 'mov' => '🎬',
        'mp3' => '🎵', 'wav' => '🎵', 'flac' => '🎵',
        'xls' => '📊', 'xlsx' => '📊', 'ppt' => '📽️', 'pptx' => '📽️'
    ];
    return $icons[strtolower($ext)] ?? '📦';
}

/**
 * 格式化文件大小
 */
function formatFileSize($bytes) {
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= (1 << (10 * $pow));
    return round($bytes, 2) . ' ' . $units[$pow];
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设置文件详情 - <?= SITE_NAME ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f5f5f7;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
        }

        .header {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .back-btn {
            padding: 10px 20px;
            background: #f5f5f7;
            color: #333;
            border: none;
            border-radius: 10px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .back-btn:hover {
            background: #e8e8ed;
        }

        .header-title h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 6px;
        }

        .header-subtitle {
            color: #666;
            font-size: 14px;
        }

        .file-info-card {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
            margin-bottom: 30px;
            display: flex;
            gap: 20px;
            align-items: center;
        }

        .file-icon {
            font-size: 64px;
        }

        .file-details h2 {
            color: #333;
            font-size: 24px;
            margin-bottom: 10px;
        }

        .file-meta {
            display: flex;
            gap: 20px;
            color: #999;
            font-size: 14px;
        }

        .file-meta-item {
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .settings-card {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
        }

        .card-title {
            font-size: 22px;
            font-weight: 600;
            color: #333;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #f0f0f0;
        }

        .section {
            margin-bottom: 32px;
        }

        .section-title {
            font-size: 18px;
            font-weight: 600;
            color: #007AFF;
            margin-bottom: 16px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .form-group {
            margin-bottom: 24px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }

        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 14px;
            font-family: inherit;
            transition: all 0.3s;
        }

        .form-group input:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #007AFF;
            box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1);
        }

        .form-group textarea {
            min-height: 100px;
            resize: vertical;
        }

        .hint {
            margin-top: 6px;
            color: #999;
            font-size: 12px;
        }

        .message {
            padding: 16px;
            border-radius: 10px;
            margin-bottom: 20px;
            font-size: 14px;
            font-weight: 500;
        }

        .message.success {
            background: #e8f5e9;
            color: #2e7d32;
            border: 1px solid #c8e6c9;
        }

        .message.error {
            background: #ffebee;
            color: #c62828;
            border: 1px solid #ffcdd2;
        }

        .actions {
            display: flex;
            gap: 12px;
            justify-content: flex-end;
            margin-top: 40px;
            padding-top: 30px;
            border-top: 2px solid #f0f0f0;
        }

        .btn {
            padding: 12px 32px;
            background: #007AFF;
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 15px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn:hover {
            background: #0056CC;
            transform: translateY(-1px);
            box-shadow: 0 4px 16px rgba(0, 122, 255, 0.3);
        }

        .btn-secondary {
            background: #f5f5f7;
            color: #333;
        }

        .btn-secondary:hover {
            background: #e8e8ed;
            transform: none;
            box-shadow: none;
        }

        .preview-section {
            background: #f5f5f7;
            border-radius: 16px;
            padding: 24px;
            margin-top: 20px;
        }

        .preview-title {
            font-size: 14px;
            font-weight: 600;
            color: #666;
            margin-bottom: 12px;
        }

        .preview-content {
            color: #333;
            font-size: 14px;
            line-height: 1.6;
        }

        @media (max-width: 768px) {
            .file-info-card {
                flex-direction: column;
                text-align: center;
            }

            .header {
                flex-direction: column;
                align-items: flex-start;
            }

            .settings-card {
                padding: 24px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <a href="index.php" class="back-btn">← 返回文件列表</a>
            <div class="header-title">
                <h1>设置文件详情</h1>
                <div class="header-subtitle">为文件配置详情页面展示信息</div>
            </div>
        </div>

        <?php if ($message): ?>
            <div class="message <?= $messageType ?>"><?= htmlspecialchars($message) ?></div>
        <?php endif; ?>

        <div class="file-info-card">
            <div class="file-icon"><?= getFileIcon($file['file_ext'] ?? '') ?></div>
            <div class="file-details">
                <h2><?= htmlspecialchars($file['original_name']) ?></h2>
                <div class="file-meta">
                    <div class="file-meta-item">
                        <span>📏</span>
                        <span><?= formatFileSize($file['file_size']) ?></span>
                    </div>
                    <div class="file-meta-item">
                        <span>📅</span>
                        <span><?= date('Y-m-d H:i', strtotime($file['upload_time'])) ?></span>
                    </div>
                    <div class="file-meta-item">
                        <span>⬇️</span>
                        <span>下载 <?= $file['download_count'] ?> 次</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="settings-card">
            <h2 class="card-title">配置详情信息</h2>

            <form method="post" action="">
                <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

                <div class="section">
                    <div class="section-title">📄 文件信息</div>

                    <div class="form-group">
                        <label>文件标题</label>
                        <input type="text" name="file_title" value="<?= htmlspecialchars($file['file_title'] ?? '') ?>" placeholder="为文件设置一个简洁的标题">
                        <div class="hint">将显示在文件详情页面上，留空则使用原始文件名</div>
                    </div>

                    <div class="form-group">
                        <label>文件描述</label>
                        <textarea name="file_description" placeholder="详细描述文件的内容、用途等..."><?= htmlspecialchars($file['file_description'] ?? '') ?></textarea>
                        <div class="hint">详细介绍文件内容，帮助用户了解文件用途</div>
                    </div>
                </div>

                <div class="section">
                    <div class="section-title">🎨 页面信息</div>

                    <div class="form-group">
                        <label>页面标题</label>
                        <input type="text" name="page_title" value="<?= htmlspecialchars($file['page_title'] ?? '') ?>" placeholder="设置详情页面的标题">
                        <div class="hint">将显示在详情页面的顶部，留空则使用文件标题</div>
                    </div>

                    <div class="form-group">
                        <label>页面描述</label>
                        <textarea name="page_description" placeholder="为详情页面写一段介绍文字..."><?= htmlspecialchars($file['page_description'] ?? '') ?></textarea>
                        <div class="hint">显示在详情页面顶部，作为整个页面的介绍</div>
                    </div>
                </div>

                <div class="actions">
                    <a href="index.php" class="btn btn-secondary">取消</a>
                    <button type="submit" class="btn">💾 保存设置</button>
                </div>
            </form>
        </div>
    </div>
</body>
</html>
